
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using JYJ.AiYard.StationManagement.TbMicroPayParams;
using JYJ.AiYard.StationManagement.TbMicroPayParams.Dtos;
using JYJ.AiYard.StationManagement.TbMicroPayParams.DomainService;
using JYJ.AiYard.StationManagement.TbMicroPayParams.Authorization;
using JYJ.AiYard.StationManagement.YardReport;
using JYJ.AiYard.StationManagement.YardCommonHald;

namespace JYJ.AiYard.StationManagement.TbMicroPayParams
{
    /// <summary>
    /// TbMicroPayParams应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class TbMicroPayParamsAppService : AiYardAppServiceBase, ITbMicroPayParamsAppService
    {
        private readonly IRepository<TbMicroPayParams, long> _entityRepository;

        private readonly ITbMicroPayParamsManager _entityManager;

        /// <summary>
        /// 构造函数 
        ///</summary>
        public TbMicroPayParamsAppService(
        IRepository<TbMicroPayParams, long> entityRepository
        ,ITbMicroPayParamsManager entityManager
        )
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
        }


        /// <summary>
        /// 获取TbMicroPayParams的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
		[AbpAuthorize(TbMicroPayParamsPermissions.Query)] 
        public async Task<PagedResultDto<TbMicroPayParamsListDto>> GetPaged(GetTbMicroPayParamssInput input)
		{
			List<long> StationList = new List<long>();
			tbQFilterText filterText = input.FilterText.ToObject<tbQFilterText>();
			if (filterText.StationId == null || filterText.StationId <= 0)
			{
				StationList = GetCurrenUserOrganizationAndChildrenUnit();
			}
			else
			{
				StationList.Add((long)filterText.StationId);
			}
			var query = _entityRepository.GetAll().Where(o => StationList.Contains(o.Station_ID));
			// TODO:根据传入的参数添加过滤条件
            

			var count = await query.CountAsync();

			var entityList = await query               
                    .OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

			// var entityListDtos = ObjectMapper.Map<List<TbMicroPayParamsListDto>>(entityList);
			var entityListDtos =entityList.MapTo<List<TbMicroPayParamsListDto>>();

			return new PagedResultDto<TbMicroPayParamsListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取TbMicroPayParamsListDto信息
		/// </summary>
		[AbpAuthorize(TbMicroPayParamsPermissions.Query)] 
		public async Task<TbMicroPayParamsListDto> GetById(EntityDto<long> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<TbMicroPayParamsListDto>();
		}

		/// <summary>
		/// 获取编辑 TbMicroPayParams
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbMicroPayParamsPermissions.Create,TbMicroPayParamsPermissions.Edit)]
		public async Task<GetTbMicroPayParamsForEditOutput> GetForEdit(NullableIdDto<long> input)
		{
			var output = new GetTbMicroPayParamsForEditOutput();
            TbMicroPayParamsEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<TbMicroPayParamsEditDto>();

				//tbMicroPayParamsEditDto = ObjectMapper.Map<List<tbMicroPayParamsEditDto>>(entity);
			}
			else
			{
				editDto = new TbMicroPayParamsEditDto();
			}

			output.TbMicroPayParams = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改TbMicroPayParams的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbMicroPayParamsPermissions.Create,TbMicroPayParamsPermissions.Edit)]
		public async Task CreateOrUpdate(CreateOrUpdateTbMicroPayParamsInput input)
		{
            if (input.TbMicroPayParams.Station_ID <= 0)
            {
                throw new UserFriendlyException("请选择确定的场所名称");
            }
            if (input.TbMicroPayParams.Id.HasValue)
			{
				await Update(input.TbMicroPayParams);
			}
			else
			{
				await Create(input.TbMicroPayParams);
			}
		}


		/// <summary>
		/// 新增TbMicroPayParams
		/// </summary>
		[AbpAuthorize(TbMicroPayParamsPermissions.Create)]
		protected virtual async Task<TbMicroPayParamsEditDto> Create(TbMicroPayParamsEditDto input)
		{
            if (string.IsNullOrEmpty(input.Client_ID.ToString()))
                input.Client_ID = 0; 

            //判断相同的客户端是否只存在一个
            var query = _entityRepository.GetAll()
                .Where(o => o.Client_ID == input.Client_ID && o.Station_ID == input.Station_ID && o.MicroPay_Type==input.MicroPay_Type);
            if (query.Count() > 0)
            {
                throw new UserFriendlyException("存在相同活动的客户端支付参数设置");
            }

            // var entity = ObjectMapper.Map <TbMicroPayParams>(input);
            var entity=input.MapTo<TbMicroPayParams>();

           
            entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<TbMicroPayParamsEditDto>();
		}

		/// <summary>
		/// 编辑TbMicroPayParams
		/// </summary>
		[AbpAuthorize(TbMicroPayParamsPermissions.Edit)]
		protected virtual async Task Update(TbMicroPayParamsEditDto input)
		{
            if (string.IsNullOrEmpty(input.Client_ID.ToString()))
                input.Client_ID = 0; 

            //判断相同的客户端是否只存在一个
            var query = _entityRepository.GetAll()
                .Where(o => o.Client_ID == input.Client_ID && o.Station_ID == input.Station_ID && o.MicroPay_Type == input.MicroPay_Type && o.Id!=input.Id);
            if (query.Count() > 0)
            {
                throw new UserFriendlyException("存在相同活动的客户端支付参数设置");
            }

            var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除TbMicroPayParams信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbMicroPayParamsPermissions.Delete)]
		public async Task Delete(EntityDto<long> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除TbMicroPayParams的方法
		/// </summary>
		[AbpAuthorize(TbMicroPayParamsPermissions.BatchDelete)]
		public async Task BatchDelete(List<long> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		///// <summary>
		///// 导出TbMicroPayParams为excel表,等待开发。
		///// </summary>
		///// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


